import socket
from socket import AF_INET, SOCK_STREAM, SOCK_DGRAM
import psutil

AD = "-"
AF_INET6 = getattr(socket, 'AF_INET6', object())
proto_map = {(AF_INET, SOCK_STREAM) : 'tcp'}

def startConnectionsScan():
	"""
	Method that verify all the connections established on local machine
	"""
	print("%-10s %-25s %-22s %-13s %-6s %s" % ("Protocol", "Local Address", "Remote Address", "Status", "PID", "Program Name"))
	
	for p in psutil.process_iter():
		name = '?'
		try:
			name = p.name
			cons = p.get_connections(kind='inet')
		except psutil.AccessDenied:
			pass	
		else:
			for c in cons:
				raddr = ""
				laddr = "%s:%s" % (c.local_address)
				if c.remote_address:
					raddr = "%s:%s" % (c.remote_address)
				print("%-10s %-25s %-22s %-13s %-6s %s" % (proto_map[(c.family, c.type)], laddr, raddr, str(c.status), p.pid, name[:15]))


	print ' '
